package com.aierkeji.call.cms.controller;

import java.net.URLEncoder;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.exception.BatchException;
import com.aierkeji.call.cms.service.AkjCallUserService;
import com.aierkeji.call.cms.util.ResultMapHelper;

@RestController
@RequestMapping("/user")
public class AkjCallUserController extends BaseController{
	private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	AkjCallUserService akjCallUserService;

	@PostMapping("/subUser")
	public Map<String, Object> subUser(@RequestBody Map<String, Object> map) {
		return ResultMapHelper.success(akjCallUserService.findCanAssigneUser(map));
	}

	@PostMapping("/list/{pageNo}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN ,Contants.ROLE_ID_ACCESS}, logical = Logical.OR)
	public Map<String, Object> list(@PathVariable("pageNo") int pageNo, @RequestBody Map<String, Object> map) {
		return ResultMapHelper.success(akjCallUserService.list(pageNo, map,false));
	}
	@PostMapping("/manager/list/{pageNo}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN ,Contants.ROLE_ID_ACCESS}, logical = Logical.OR)
	public Map<String, Object> managerList(@PathVariable("pageNo") int pageNo, @RequestBody Map<String, Object> map) {
		return ResultMapHelper.success(akjCallUserService.list(pageNo, map,true));
	}

	@PostMapping("/resetPassword")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER , Contants.ROLE_ID_ADMIN,Contants.ROLE_ID_ACCESS}, logical = Logical.OR)
	public Map<String, Object> resetPassword(@RequestBody Map<String, Object> map) {
		return akjCallUserService.resetPassword(map);
	}

	@PostMapping("/delete/{id}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN ,Contants.ROLE_ID_ACCESS}, logical = Logical.OR)
	public Map<String, Object> delete(@PathVariable("id") int id) {
		return akjCallUserService.delete(id);
	}

	@PostMapping("/edit")
	public Map<String, Object> edit(@RequestBody Map<String, Object> map) {
		return akjCallUserService.edit(map);
	}

	@PostMapping("/editPassword")
	public Map<String, Object> editPassword(@RequestBody Map<String, Object> map) {
		return akjCallUserService.editPassword(map);
	}

	@GetMapping("/getAllRoles")
	public Map<String, Object> getAllRoles() {
		return akjCallUserService.getAllRoles();
	}

	@GetMapping("/downloadAddUserTemplete")
	public ResponseEntity downloadAddUserTemplete() throws Exception {
		byte[] bytes = null;
		HttpStatus statusCode = HttpStatus.OK;
		HttpHeaders headers = new HttpHeaders();
		Map<String, Object> resultMap = null;
		try {
			resultMap = akjCallUserService.downloadAddUserTemplete();
		} catch (Exception e) {
			log.error("导出线索列表错误:", e);
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>("发生未知错误请联系管理员!", headers, statusCode);
			return entity;
		}
		if (ResultMapHelper.isSuccess(resultMap)) {
			bytes = resultMap.get("data").toString().getBytes("GBK");
		} else {
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>((String) resultMap.get("msg"), headers,
					statusCode);
			return entity;
		}
		headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode("批量新增下级模板", "UTF-8") + ".csv");
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, statusCode);
		return entity;
	}

	@PostMapping("/addUser")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN,
			Contants.ROLE_ID_ACCESS }, logical = Logical.OR)
	public Map<String, Object> addUser(@RequestBody Map<String, Object> map) {
		return akjCallUserService.addUser(map);
	}

	@PostMapping("/batchAddUser")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN,
			Contants.ROLE_ID_STATISTICIAN }, logical = Logical.OR)
	public Map<String, Object> batchAddUser(@RequestParam(name = "file", required = false) MultipartFile file) {
		 try {
			return akjCallUserService.batchAddUser(file);
		} catch (BatchException e) {
			// TODO Auto-generated catch block
			log.error(" batch save error", e);
			return ResultMapHelper.failure(e.getMessage());
		}
		 
	}
}
